package com.antilia.infomaresme;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.Application;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.guice.GuiceComponentInjector;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.util.time.Duration;
import org.myapp.gae.GaeSafeServletWebRequest;


/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see com.wiquery.plugins.test.Start#main(String[])
 */
public class InfomaresmeApplication extends WebApplication 
{    
	

	private boolean isProd =
        "Production".equalsIgnoreCase(
            System.getProperty("com.google.appengine.runtime.environment"));
	
    /**
     * Constructor
     */
	public InfomaresmeApplication()
	{
	}
	
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<? extends WebPage> getHomePage()	
	{
		return com.antilia.infomaresme.pages.HomePage.class;
	}
	
	@Override
	protected void init() {
		getDebugSettings().setOutputMarkupContainerClassName(false);
		getMarkupSettings().setStripWicketTags(true);
		super.init();
		
		
		/*
		AUTORIZATION_STRATEGY = new DisableProtectedAuthorizationStrategy() {
			
			public java.lang.Class<? extends org.apache.wicket.markup.html.WebPage> getSignInPage() {
				return InqleQaApplication.this.getSignInPage();
			};
		};
		
		getSecuritySettings().setUnauthorizedComponentInstantiationListener(AUTORIZATION_STRATEGY);
		getSecuritySettings().setAuthorizationStrategy(AUTORIZATION_STRATEGY);
		getApplicationSettings().setAccessDeniedPage(getSignInPage());
		getApplicationSettings().setPageExpiredErrorPage(getSignInPage());
		*/
		
		// for Google App Engine
        getResourceSettings().setResourcePollFrequency(null);
        if(isInDevelopmentMode())
        	getResourceSettings().setResourceWatcher(new ModificationWatcher());

        // Enable Guice for field injection on Wicket pages.  Unfortunately, constructor injection into
        // pages is not supported.  Supplying ServletModule is optional; it enables usage of @RequestScoped and
        // @SessionScoped, which may not be useful for Wicket applications because the WebPage instances are
        // already stored in session, with their dependencies injected once per session.
        //addComponentInstantiationListener(new GuiceComponentInjector(this, new GuiceModule()));
        //       addComponentInstantiationListener(new GuiceComponentInjector(this, new ServletModule(), new GuiceModule()));
        addComponentInstantiationListener(new GuiceComponentInjector(this, new Module()));
        InjectorHolder.getInjector().inject(this);	
	}

	
	protected WebRequest newWebRequest(HttpServletRequest servletRequest) {
		if(isInDevelopmentMode())
			getResourceSettings().getResourceWatcher(true).start(Duration.ONE_SECOND);
		
		return new GaeSafeServletWebRequest(servletRequest);
	}

	 
	public static InfomaresmeApplication getQAApplication() {
		return (InfomaresmeApplication)WebApplication.get();
	}
	
	@Override
	protected ISessionStore newSessionStore()
	{
		// for Google App Engine
		return new HttpSessionStore(this);
	}
	
	@Override
	public Session newSession(Request request, Response response) {
		return new InfomaresmeSession(request);
	}

	
	
	protected boolean isInDevelopmentMode() {
		return !isProd;
	}
	
	@Override
    public String getConfigurationType() {
        return (isProd ? Application.DEPLOYMENT : Application.DEVELOPMENT);
    }

}

/*
 * Copyright (c) 2010 INQLING, LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

